package com.wxsoft.business.physic.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wxsoft.business.common.controller.BaseController;
import com.wxsoft.business.model.easyui.DataGrid;
import com.wxsoft.business.model.easyui.PageHelper;

import com.wxsoft.business.physic.service.IPsPhysicInHeadService;
import com.wxsoft.business.physic.pojo.PsPhysicInHead;
import com.wxsoft.business.sys.pojo.SysUser;
import com.wxsoft.util.UserUtil;

@Controller
@RequestMapping("/psPhysicInHead")
public class PsPhysicInHeadController extends BaseController{
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Resource
	private IPsPhysicInHeadService service;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		return "physic/psPhysicInHeadlist";
	}
	
    @ResponseBody
	@RequestMapping("/datagrid")
	public DataGrid list(HttpServletRequest request,PageHelper page, PsPhysicInHead psPhysicInHead,HttpServletResponse response) throws IOException{
		
		DataGrid dg = new DataGrid();
		dg.setTotal(service.findCount(psPhysicInHead));
		List<PsPhysicInHead> psPhysicInHeadList = service.findAll(page, psPhysicInHead);
		dg.setRows(psPhysicInHeadList);

		return dg ;
	}

    /**
     * 
     * @Description:TODO
     * @author: chenliang
     * @time:2018-3-18 下午5:57:39
     */
	@RequestMapping(value = "/toAdd", method = RequestMethod.GET)
	public String toAdd(Model model) {
		return "physic/in/_form";
	}
	
	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public void add(HttpServletRequest request,HttpServletResponse response, PsPhysicInHead psPhysicInHead) {
		try {
			SysUser user = UserUtil.getUser(request);
			psPhysicInHead.setHandle(user.getUser_id());
			psPhysicInHead.setHandle_date(new Date());
			psPhysicInHead.setUnit_id(user.getUnit_id());
			String r = service.add(psPhysicInHead);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response , false, e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public void modify(HttpServletRequest request,HttpServletResponse response, PsPhysicInHead psPhysicInHead) {
		try {
			SysUser user = UserUtil.getUser(request);
			psPhysicInHead.setHandle(user.getUser_id());
			psPhysicInHead.setHandle_date(new Date());
			String r = service.modify(psPhysicInHead);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, false, e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(HttpServletRequest request,HttpServletResponse response, PsPhysicInHead psPhysicInHead) {
		try {
			String r = service.delete(psPhysicInHead);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, false, e.getMessage());
		}
	}
}
